<!-- your_app/templates/fortune.html -->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>出生日期分析查询</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: "Microsoft YaHei", sans-serif;
        }
        .container {
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #555;
        }
        input, select {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }
        button {
            width: 100%;
            padding: 15px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 18px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #45a049;
        }
        .result-box {
            margin-top: 30px;
            padding: 20px;
            border-radius: 5px;
            background-color: #f9f9f9;
            border: 1px solid #eee;
        }
        .error {
            color: #dc3545;
            text-align: center;
            margin-bottom: 20px;
            font-size: 16px;
        }
        .success {
            color: #28a745;
            line-height: 1.8;
            font-size: 16px;
        }
        @keyframes spin {
          0% { transform: rotate(0deg); }
          100% { transform: rotate(360deg); }
        }
        #resultContent {
          max-height: 120px;
          overflow: hidden;
          position: relative;
          padding-bottom: 20px;
        }
        #resultContent::after {
          content: '';
          position: absolute;
          bottom: 0;
          left: 0;
          width: 100%;
          height: 40px;
          background: linear-gradient(to bottom, transparent, #f9f9f9); /* 修正为result-box背景色 */
          display: block;
        }
        #resultContent.expand {
          max-height: none;
        }
        #resultContent.expand::after {
          display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>出生日期分析查询</h1>
        <!-- 在表单中插入 CSRF 标签，确保服务端设置 csrftoken Cookie -->
        <!-- 注意：{\% csrf_token \%} 是模板标签，只有在模板被 Django 渲染时才生效 -->
        <!-- 补充form标签，语义更完整 -->
        <form>
            {% csrf_token %}
            <div class="form-group">
                <label for="birth_date">出生日期</label>
                <input type="date" id="user_birthday_date" name="user_birthday_date" required>
            </div>

            <div class="form-group">
                <label for="birth_time">出生时间</label>
                <input type="time" id="user_birthday_time" name="user_birthday_time" required>
            </div>

            <div class="form-group">
                <label for="user_sex">性别</label>
                <select id="user_sex" name="user_sex" required>
                    <option value="">请选择</option>
                    <option value="1">男</option>
                    <option value="0">女</option>
                </select>
            </div>

            <div class="form-group">
                <label for="user_address">地区</label>
                <select id="user_address" name="user_address" required>
                    <option value="上海">上海</option>
                    <option value="北京">北京</option>
                    <option value="广州">广州</option> <!-- 修正重复的北京 -->
                </select>
            </div>

            <button type="button" onclick="sub()">查询分析</button> <!-- 改为type="button"，避免表单默认提交 -->
        </form>

        <div class="loading-box" id="loadingBox" style="display: none; text-align: center; padding: 30px 0;">
          <div style="width: 40px; height: 40px; border: 4px solid #eee; border-top: 4px solid #3498db; border-radius: 50%; margin: 0 auto; animation: spin 1s linear infinite;"></div>
          <p style="margin-top: 10px; color: #666;">正在查询分析，请稍候...</p>
        </div>

        <div class="result-box" id="resultBox" style="display: none;">
          <h3 style="margin-bottom: 15px; color: #333;">分析结果</h3>
          <div class="success" id="resultContent"></div>
          <input type="hidden" id="data_id" name="data_id" value="">
          <button type="button" style="display:none; margin-top: 20px;" id="buy" >购买后查看详情</button>
          <button type="button" style="display:none; margin-top: 20px;" id="down">下载</button>
        </div>
    </div>

<script src="/static/js/axios.min.js"></script>
<script>
    // 提前获取DOM元素，避免重复查询
    const loadingBox = document.getElementById('loadingBox');
    const resultBox = document.getElementById('resultBox');
    const downBtn = document.getElementById('down');
    const buyBtn = document.getElementById('buy');
    const resultContent = document.getElementById('resultContent');
    // CSRF 由 axios 的 xsrf 配置自动处理，无需手动读取 Cookie
    // 简化：使用 axios 的 XSRF 自动处理（同源情况下无需手动读取 Cookie）
    axios.defaults.xsrfCookieName = 'csrftoken';
    axios.defaults.xsrfHeaderName = 'X-CSRFToken';

    // 购买按钮点击事件（提前绑定，无需在sub函数内重复写）
    buyBtn.onclick = function() {
        const data_id = document.getElementById('data_id').value;

        let url='/api/alipay/'
        axios
          .get(url, { params: {'data_id': data_id} })
          .then(response => {
            console.log(response.data);
            window.location.href=response.data
          })
          .catch(error=>{
              console.log(error)
          })

        resultContent.classList.add('expand');
        this.style.display = 'none';
        downBtn.style.display = 'block';

    };

    function sub() {
        // 显示加载状态
        loadingBox.style.display = 'block';
        resultBox.style.display = 'none'; // 隐藏之前的结果

        // 收集表单数据（移除X-CSRFToken，放到请求头）
        const formData = {
            'user_birthday_date': document.getElementById('user_birthday_date').value,
            'user_birthday_time': document.getElementById('user_birthday_time').value,
            'user_sex': document.getElementById('user_sex').value,
            'user_address': document.getElementById('user_address').value
        };

        console.log('请求数据：', formData);
        const URL = '/api/userinfo/';

        axios.post(URL, formData)
        .then(response => {
            const result = response.data.result;
            console.log(response.data.data_id)
            document.getElementById('resultContent').value=response.data.data_id
            resultContent.innerHTML = result || '暂无分析结果';
            loadingBox.style.display = 'none';
            resultBox.style.display = 'block';
            // 控制按钮显示
            if (result && result.trim() !== '') {
                buyBtn.style.display = 'block';
                downBtn.style.display = 'none';
                resultContent.classList.remove('expand'); // 重置展开状态
            } else {
                buyBtn.style.display = 'none';
                downBtn.style.display = 'none';
            }
        })
        .catch(err => {
            loadingBox.style.display = 'none';
            console.error('查询失败：', err.response?.data || err.message);
            resultBox.style.display = 'block';
            resultContent.innerHTML = '<span style="color: #dc3545;">查询失败，请稍后重试</span>';
        });
    }
</script>
</body>
</html>